import requests
import json


def request_send_msg(
    webhook="https://oapi.dingtalk.com/robot/send?"
    "access_token=4ab975feb923dc37d5c6009845989dab6879f09633a870a7c2915a872559e294",
    title="新咨询",
    text="1111 @015320154417847317  你好",
    userids=["015320154417847317"],
):
    request_headers = {
        "Content-Type": "application/json",
        "Accept": "*/*",
    }
    values = {
        "msgtype": "markdown",
        "markdown": {
            "title": title,
            "text": text,
        },
        "at": {
            "atUserIds": userids,
        },
        "isAtAll": False,
    }
    try:
        response = requests.post(
            webhook, headers=request_headers, data=json.dumps(values)
        )
        response.raise_for_status()
    except Exception as e:
        print(e)
        return None
    return response.json()


def request_send_text_msg(
    webhook="https://oapi.dingtalk.com/robot/send?access_token=71e511c95920b61145a6ee7777fc90190bde10d1a4acda79c588569f84bde375",
    text="你好",
    userids=["015320154417847317"],
):
    request_headers = {
        "Content-Type": "application/json",
        "Accept": "*/*",
    }
    values = {
        "msgtype": "text",
        "text": {
            "content": text,
        },
        "at": {
            "atUserIds": userids,
        },
        "isAtAll": False,
    }
    try:
        response = requests.post(
            webhook, headers=request_headers, data=json.dumps(values)
        )
        response.raise_for_status()
    except Exception as e:
        print(e)
        return None
    return response.json()


if __name__ == "__main__":
    # res = request_send_msg(
    #     title="测试标题",
    #     text="测试内容 @015320154417847317 你好",
    #     userids=["015320154417847317"],
    # )
    # print(res)
    res = request_send_text_msg(
        webhook="https://oapi.dingtalk.com/robot/send?access_token=71e511c95920b61145a6ee7777fc90190bde10d1a4acda79c588569f84bde375",
        text="测试文本消息 @015320154417847317 你好",
        userids=["015320154417847317"],
    )
    print(res)
